package com.cwd.arm.infrastructure.dao.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cwd.arm.common.util.SessionUtil;
import com.cwd.arm.infrastructure.dao.MsgRecipientDao;
import com.cwd.arm.infrastructure.dao.base.DaoImpl;
import com.cwd.arm.infrastructure.mapper.MsgRecipientMapper;
import com.cwd.arm.infrastructure.po.MsgRecipientPo;
import org.springframework.stereotype.Service;

import java.util.List;

/**
* Description:消息接受者
* Created on 2022年01月15日
* @author  yan
*/
@Service
public class MsgRecipientDaoImpl extends DaoImpl<MsgRecipientMapper, MsgRecipientPo> implements MsgRecipientDao {

    @Override
    public void readByCurrentUser(Long msgId) {
        QueryWrapper<MsgRecipientPo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("msg_id",msgId);
        queryWrapper.eq("recipient_id", SessionUtil.getUserId());
        MsgRecipientPo msgRecipientPo = getOne(queryWrapper);
        if(!msgRecipientPo.getHasRead()){
            msgRecipientPo.setHasRead(true);
            updateById(msgRecipientPo);
        }
    }

    @Override
    public List<MsgRecipientPo> selectByMsgId(Long msgId) {
        QueryWrapper<MsgRecipientPo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("msg_id",msgId);
        return baseList(queryWrapper);
    }

    @Override
    public void removedByRecipientIdsAndMsgId(Long msgId, List<Long> recipientIds) {
        for(Long recipientId : recipientIds){
            QueryWrapper<MsgRecipientPo> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("msg_id",msgId);
            queryWrapper.eq("recipient_id",recipientId);
            remove(queryWrapper);
        }
    }
}
